﻿package cn.poco.pepsi.util.xmlpared;

import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;
import cn.poco.pepsi.bean.Guest;

/**
 * 解析同城食客接口
 * 
 * @author tanglx
 * 
 */
public class GuestXmlparse {

	private static ArrayList<Guest> guests = null;
	private static Guest guestBean = null;

	public static ArrayList<Guest> getXml(InputStream inSteam) throws Exception {

		XmlPullParser xmpparse = Xml.newPullParser(); // 得到解释器
		xmpparse.setInput(inSteam, "UTF-8"); // 设置解释流
		int code = xmpparse.getEventType(); // 得到节点信息
		while (code != XmlPullParser.END_DOCUMENT) {
			switch (code) {
			case XmlPullParser.START_DOCUMENT:
				guests = new ArrayList<Guest>();
				break;
			case XmlPullParser.START_TAG:
				if ("grade".equals(xmpparse.getName())) {
					guestBean = new Guest();
				}
				if (guestBean != null) {

					if ("user-id".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						guestBean.setUseId(key);
					}
					if ("user-name".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						guestBean.setUseName(key);
					}
					if ("user-icon".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						guestBean.setUseIcon(key);
					}
					if ("res-id".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						guestBean.setResId(key);
					}
					if ("res-title".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						guestBean.setResTitle(key);
					}
					if ("grade-id".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						guestBean.setGradeId(key);
					}
					if ("add-time".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						guestBean.setAddTime(key);
					}
					if ("source-url".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						guestBean.setSourceUrl(key);
					}
					if ("image-url".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						guestBean.setImageUrl(key);
					}
					if ("image-id".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						if (key != null && !key.equals("")) {
							guestBean.setImageId(Integer.parseInt(key));
						}
					}
					if ("grade-content".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						guestBean.setGradeContent(key);
					}
					if ("vote-cnt".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						if (key == null || key.equals("")) {
							key = "0";
						}
						guestBean.setVoteCnt(key);
					}
					if ("service".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						if (key != null && !key.equals("")) {
							if (key.startsWith("-")) {
								guestBean.setService(key);
							} else {
								guestBean.setService(key.substring(0, 1));
							}
						}
					}
					if ("produce".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						if (key != null && !key.equals("")) {
							if (key.startsWith("-")) {
								guestBean.setProduce(key);
							} else {
								guestBean.setProduce(key.substring(0, 1));
							}
						}
					}

					if ("average".equals(xmpparse.getName())) {
						String key = xmpparse.nextText();
						if (key == null || key.equals("")) {
							key = "0";
						}
						guestBean.setAverage(key);
					}
				}
				break;
			case XmlPullParser.END_TAG:
				if ("grade".equals(xmpparse.getName())) {
					guests.add(guestBean);
					guestBean = null;
				}
				break;
			}
			code = xmpparse.next();
		}
		inSteam.close();
		return guests;
	}
}
